package global

import (
	"database/sql"
	"time"
)

// ScheduleType 表示调度类型
type ScheduleType string

type TimeDifferenceResultS struct {
	Second int64
	Minute int64
	Hour   int64
	Day    int64
	Week   int64
	Month  int64
	Year   int64
}

const (
	// 定义调度类型
	Secondly ScheduleType = "SECOND"
	Minutely ScheduleType = "MINUTE"
	Hourly   ScheduleType = "HOUR"
	Daily    ScheduleType = "DAI"
	Weekly   ScheduleType = "WEEK"
	Monthly  ScheduleType = "MONTH"
	Yearly   ScheduleType = "YEAR"
)

// JobInterval 表示任务的调度间隔
type JobInterval struct {
	Frequency     ScheduleType // 调度频率
	Interval      int          // 调度的间隔
	TimeFrequency TimeDifferenceResultS
	ByDay         []time.Weekday // 适用于 WEEKLY 和 MONTHLY
	ByHour        []int          // 适用于 DAILY、WEEKLY、MONTHLY (1-24)
	ByMinute      []int          // 适用于每小时(1-60)
	BySecond      []int          // 适用于每分钟(1-60)
	ByMonthDay    []int          // 适用于 YEARLY，表示每月的哪一天（1-31）
	ByMonth       []int          // 适用于 YEARLY，表示月份（1-12）
	BySetPos      int            // 特定位置
}
type JobBodyPrepareS struct {
	ExecType   string //记录执行的本体是函数、还是dbms包、dblink、还是常规的sql语句
	BodyOld    string //原文主体
	BodyModify string
}

// JobDefinitionConvertS 代表一个定时任务
type JobDefinitionConvertS struct {
	Name        string `json:"jobName"`    // 任务名称
	Description string `json:"jobComment"` // 任务描述
	StartTime   string `json:"startTime"`
	BodyPrepare []JobBodyPrepareS
	JobStatus   string
	PrintInfo   string
	IntervalOld string
	Interval    JobInterval `json:"interval"` // 调度间隔
	LastRun     time.Time   // 上次执行时间
	NextRun     time.Time   // 下次执行时间
}

type JobResultDefinition struct {
	Name          string `json:"jobName"`
	Type          string `json:"type"`
	Action        string `json:"jobDefinition"`
	LastTime      string `json:"lastDate"`
	NextTime      string `json:"nextDate"`
	StartTime     string `json:"startDate"`
	Interval      string `json:"interval"`
	IntervalField string `json:"intervalField"`
	IntervalValue string `json:"intervalValue"`
	Enable        string `json:"enable"`
	State         string `json:"state"`
	Comment       string `json:"comment"`
	PrintInfo     string
	JobPackage    string
}
type ViewResultDefinition struct {
	BaseResult     ReturnBaseResult
	Schema       string   `json:"schema"`
	Name         string   `json:"viewName"`
	Column       []string `json:"column"`
	Definer      string   `json:"definer"`
	SecurityType string   `json:"securityType"`
	Type         string   `json:"type"`
	BodyText     string   `json:"bodyText"`
	Action       string   `json:"createView"`
	PrintInfo    string
}
type SeqResultDefinition struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	MinValue    string `json:"minValue"`
	MaxValue    string `json:"maxValue"`
	IncrementBy string `json:"incrementBy"`
	CycleFlag   string `json:"cycleFlag"`
	OrderFlag   string `json:"orderFlag"`
	CacheSize   string `json:"cacheSize"`
	StartValue  string `json:"startValue"`
	Type        string `json:"type"`
	PrintInfo   string //创建语句
}

type ViewDefinitionConvertS struct {
	Schema       string   `json:"schema"`
	Name         string   `json:"viewName"` // 任务名称
	Column       []string `json:"column"`
	Definer      string   `json:"definer"`
	SecurityType string   `json:"securityType"`
	Description  string   `json:"jobComment"` // 任务描述
	BodyText     string   `json:"bodyText"`
	BodyPrepare  []JobBodyPrepareS
	PrintInfo    string
}
type SeqDefinitionConvertS struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	MinValue    string `json:"minValue"`
	MaxValue    string `json:"maxValue"`
	IncrementBy string `json:"incrementBy"`
	CycleFlag   string `json:"cycleFlag"`
	OrderFlag   string `json:"orderFlag"`
	CacheSize   string `json:"cacheSize"`
	StartValue  string `json:"startValue"`
	Type        string `json:"type"`
	PrintInfo   string
}

type TriggerResultDefinition struct {
	Schema        string `json:"schema"`
	Name          string `json:"name"`
	Definer       string `json:"definer"`
	TriggerType   string `json:"triggerType"`
	MonitorEvent  string `json:"monitorEvent"`
	Orientation   string `json:"orientation"` //方向  ROWS or STATEMENT
	Timing        string `json:"timing"`      //定时  BEFORE or AFTER
	OldValue      string `json:"oldValue"`    //是否用到旧值
	NewValue      string `json:"newValue"`    //是否用到新值
	MonitorSchema string `json:"monitorSchema"`
	MonitorTable  string `json:"monitorTable"`
	CaseWhen      string `json:"caseWhen"`
	Comment       string `json:"comment"`
	Body          string `json:"body"`
	Frequency     string `json:"frequency"`
	Status        string `json:"status"`
	Type          string `json:"type"`
	PrintInfo     string
}
type TriggerDefinitionConvertS struct {
	Schema            string `json:"schema"`
	Name              string `json:"name"`
	TriggerEventGarth []string
	Orientation       string `json:"orientation"` //方向  ROWS or STATEMENT
	Timing            string `json:"timing"`      //定时  BEFORE or AFTER
	TriggerType       string `json:"triggerType"`
	MonitorEvent      string `json:"monitorEvent"`
	MonitorTable      string `json:"monitorTable"`
	CaseWhen          string `json:"caseWhen"`
	Comment           string `json:"comment"`
	Body              string `json:"body"`
	Frequency         string `json:"frequency"`
	Status            string `json:"status"`
	Type              string `json:"type"`
	PrintInfo         string
}
type ForeignResultDefinition struct {
	Schema      string   `json:"schema"`
	RSchema     string   `json:"rSchema"`
	TableName   string   `json:"table"`
	RTableName  string   `json:"refTable"`
	ForeignName string   `json:"name"`
	RName       string   `json:"rName"`
	ForeignKey  []string `json:"foreignKey"`
	RColumn     []string `json:"refColumn"`
	Type        string   `json:"type"`
	PrintInfo   string
}
type ForeignDefinitionConvertS struct {
	Schema      string   `json:"schema"`
	RSchema     string   `json:"rSchema"`
	TableName   string   `json:"table"`
	RTableName  string   `json:"refTable"`
	ForeignName string   `json:"name"`
	RName       string   `json:"rName"`
	ForeignKey  []string `json:"foreignKey"`
	RColumn     []string `json:"refColumn"`
	Type        string   `json:"type"`
	PrintInfo   string
}
type FuncInputOutParameter struct {
	PName      string `json:"pName"`
	PType      string `json:"pType"`
	PDirection string `json:"pDirection"`
}
type FuncResultDefinition struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	Body        string `json:"body"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type FuncDefinitionConvertS struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	Body        string `json:"body"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type ProcedureResultDefinition struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	Body        string `json:"body"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type ProcedureDefinitionConvertS struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	Body        string `json:"body"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type PackageResultDefinition struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	HeadText    string `json:"headText"`
	BodyText    string `json:"bodyText"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type PackageDefinitionConvertS struct {
	Schema      string `json:"schema"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"`
	HeadText    string `json:"headText"`
	BodyText    string `json:"bodyText"`
	Comment     string `json:"comment"`
	PInput      []FuncInputOutParameter
	POutput     []FuncInputOutParameter
	Type        string `json:"type"`
	PrintInfo   string
}
type TypeResultDefinition struct {
	Schema    string `json:"schema"`
	Name      string `json:"name"`
	Body      string `json:"body"`
	Comment   string `json:"comment"`
	Type      string `json:"type"`
	PrintInfo string
}
type TypeDefinitionConvertS struct {
	Schema    string `json:"schema"`
	Name      string `json:"name"`
	Body      string `json:"body"`
	Comment   string `json:"comment"`
	Type      string `json:"type"`
	PrintInfo string
}
type JobObjectResult struct {
	Definition JobResultDefinition   `json:"definition"`
	Convert    JobDefinitionConvertS `json:"convert"`
}
type ViewObjectResult struct {
	Definition ViewResultDefinition   `json:"definition"`
	Convert    ViewDefinitionConvertS `json:"convert"`
}
type SeqObjectResult struct {
	Definition SeqResultDefinition   `json:"definition"`
	Convert    SeqDefinitionConvertS `json:"convert"`
}
type TriggerObjectResult struct {
	Definition TriggerResultDefinition   `json:"definition"`
	Convert    TriggerDefinitionConvertS `json:"convert"`
}
type ForeignObjectResult struct {
	Definition ForeignResultDefinition   `json:"definition"`
	Convert    ForeignDefinitionConvertS `json:"convert"`
}
type FuncObjectResult struct {
	Definition FuncResultDefinition   `json:"definition"`
	Convert    FuncDefinitionConvertS `json:"convert"`
}
type ProcedureObjectResult struct {
	Definition ProcedureResultDefinition   `json:"definition"`
	Convert    ProcedureDefinitionConvertS `json:"convert"`
}
type PackageObjectResult struct {
	Definition PackageResultDefinition   `json:"definition"`
	Convert    PackageDefinitionConvertS `json:"convert"`
}
type TypeObjectResult struct {
	Definition TypeResultDefinition   `json:"definition"`
	Convert    TypeDefinitionConvertS `json:"convert"`
}

type ObjectFixSqlS struct {
	CreateSql []string
}
type SchemaObjectReturnResult struct {
	BaseResult    ReturnBaseResult
	JobResult       JobObjectResult
	ViewResult      ViewObjectResult
	SeqResult       SeqObjectResult
	TriggerResult   TriggerObjectResult
	ForeignResult   ForeignObjectResult
	FuncResult      FuncObjectResult
	ProcedureResult ProcedureObjectResult
	PackageResult   PackageObjectResult
	TypeResult      TypeObjectResult
	FixSql          ObjectFixSqlS
}
type SchemaObjectInput struct {
	Schema, Table    string
	DB               map[string]*sql.DB
	Type             string //object Type job or func or view
	Input            StartPartConfigInputP
	ObjectDefinition SchemaObjectReturnResult
}
